import { Injectable } from '@angular/core';
import { HttpClient ,HttpErrorResponse} from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError, retry } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class BackendService {
  constructor(private http: HttpClient) { };
  username:string;
  login(param):Observable<any>{
    console.log("backends http login",param);
    return this.http.post("./login",param)
    .pipe(
      catchError(this.handleError)
    );
  }
  saveExtinfo(param):Observable<any>{
    console.log("backends http saveExtinfo",param);
    return this.http.post("./saveExtinfo",param)
    .pipe(
      catchError(this.handleError)
    );
  }
  getExtinfo(param):Observable<any>{
    console.log("backends http getExtinfo",param);
    return this.http.post("./getExtinfo",param)
    .pipe(
      catchError(this.handleError)
    );
  }
  searchFile(param):Observable<any>{
    console.log("backends http searchFile",param);
    return this.http.post("./searchFile",param)
    .pipe(
      catchError(this.handleError)
    );
  }
  searhFileFromExtinfo(param):Observable<any>{
    console.log("backends http searhFileFromExtinfo",param);
    return this.http.post("./searhFileFromExtinfo",param)
    .pipe(
      catchError(this.handleError)
    );
  }

  
  ////////////////////////////////////////////
  initialData():Observable<any>{
    console.log("backends http initialData");
    return this.http.get("./initialData") 
    .pipe(
      catchError(this.handleError)
    );
  }
  getYwlxTree(fileKey:string):Observable<any>{
    return this.http.get("./getYwlxTree?filekey="+fileKey) 
    .pipe(
      catchError(this.handleError)
    );
  }
  
  nodeAddFile(param):Observable<any>{
    console.log("backends http nodeAddFile",param);
    return this.http.post("./nodeAddFile",param) 
    .pipe(
      catchError(this.handleError)
    );
  }
  nodeDelFile(param):Observable<any>{
    console.log("backends http nodeDelFile",param);
    return this.http.post("./nodeDelFile",param) 
    .pipe(
      catchError(this.handleError)
    );
  }
  getChildren(param):Observable<any>{
    console.log("backends http getchildren",param);
    return this.http.post("./getchildren",param) 
    .pipe(
      catchError(this.handleError)
    );
  }
  renameNode(param):Observable<any>{
    console.log("backends http renameNode",param);
    return this.http.post("./renamenode",param) 
    .pipe(
      catchError(this.handleError)
    );
  }
  addNode(param):Observable<any>{
    console.log("backends http addnode",param);
    return this.http.post("./addnode",param) 
    .pipe(
      catchError(this.handleError)
    );
  }
  delNode(param):Observable<any>{
    console.log("backends http delnode",param);
    return this.http.post("./delnode",param) 
    .pipe(
      catchError(this.handleError)
    );
  }
  exportData(param):Observable<any>{
    console.log("backends http exportNode",param);
    return this.http.post("./exportnode",param) 
    .pipe(
      catchError(this.handleError)
    );
  }
  copyFile(param):Observable<any>{
    console.log("backends http copyFile",param);
    return this.http.post("./copyfile",param) 
    .pipe(
      catchError(this.handleError)
    );
  }
  
  exportFiles(param):Observable<any>{
    console.log("backends http exportFiles",param);
    return this.http.post("./exportFiles",param) 
    .pipe(
      catchError(this.handleError)
    );
  }
  importData(param):Observable<any>{
    console.log("backends http importData",param);
    return this.http.post("./importdata",param) 
    .pipe(
      catchError(this.handleError)
    );
  }
  addFile(param):Observable<any>{
    console.log("backends http addFile",param);
    return this.http.post("./addfile",param) 
    .pipe(
      catchError(this.handleError)
    );
  }
  private handleError(error: HttpErrorResponse) {
    if (error.error instanceof ErrorEvent) {
      // A client-side or network error occurred. Handle it accordingly.
      console.error('An error occurred:', error.error.message);
    } else {
      // The backend returned an unsuccessful response code.
      // The response body may contain clues as to what went wrong,
      console.error(
        `Backend returned code ${error.status}, ` +
        `body was: ${error.error}`);
    }
    // return an observable with a user-facing error message
    return throwError(
      'Something bad happened; please try again later.');
  };
}
